//
//  XLRefresh.swift
//  lianxi
//
//  Created by macbook on 16/7/1.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit


//刷新状态
enum refreshType: Int{

    //正常
    case Normal = 0
    //下拉
    case Pulling = 1
    //刷新中
    case Refreshing = 2
}

let RefaeshHight:CGFloat = 50

class XLRefresh: UIControl {

    //创建一个被观察着
    var scrollView:UIScrollView?
    var refreshState:refreshType = .Normal{
    
        didSet{
        
            switch refreshState {
            case .Normal:
                label.text = "正常"
                
                //如果我们上一个动作为刷新中才开始动画
                if oldValue == .Refreshing {
                    
                    UIView.animateWithDuration(0.25, animations: { 
                        
                       self.scrollView?.contentInset.top -= RefaeshHight
                    })
                }
            case .Pulling:
                label.text = "下拉中"
            case .Refreshing:
                label.text = "刷新中"
                //设置动画
                UIView.animateWithDuration(0.25, animations: { 
                    
                    self.scrollView?.contentInset.top += RefaeshHight
                    }, completion: { (_) in
                        
                        //发送监听 监听值的改变
                        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
                })
            }
        }
    }
    
    
    //监听添加在哪个类上
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        //判断是否有值 而且是否可以滚动
        guard let scrollView = newSuperview as? UIScrollView else
        {
            return
        }
        
        //给观察者赋值
        self.scrollView = scrollView
        
        /**
         Observer                观察者
         
         - parameter forKeyPath: 属性
         - parameter options:    属性变化 old new
         
         
        
         */
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
    //观察着实现
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let contentOffSetY = self.scrollView?.contentOffset.y ?? 0
        //判断scrollView是否在滚动
        let contentInsetTop = self.scrollView!.contentInset.top
        
        //拖拽中
        if scrollView!.dragging {
            
            if contentOffSetY > -contentInsetTop - RefaeshHight && refreshState == .Pulling
            {
                //正常
                refreshState = .Normal
            }else if contentOffSetY <= -contentInsetTop - RefaeshHight && refreshState == .Normal
            {
                //下拉中
                refreshState = .Pulling
            }
        }else
            {
                //用户松手
                //如果用户停止拖动 而且松手 而且控件状态为下拉中
                if refreshState == .Pulling {
                    
                    refreshState = .Refreshing
                }
            }
        }
    
    
    
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: -RefaeshHight, width: KScreenW, height: RefaeshHight))
        setupUI()
    }
    
    
    //外界调用
    func endRefreshing(){
    
        refreshState = .Normal
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI(){
    
        backgroundColor = UIColor.orangeColor()
        self.addSubview(label)
    
    }
    
    
    
    private lazy var label:UILabel = {
        
        let lab = UILabel()
        lab.text = "刷新"
        lab.font = UIFont.systemFontOfSize(15)
        lab.textAlignment = .Center
        lab.frame = CGRect(x: 0, y: 0, width: KScreenW, height: RefaeshHight)
        
       return lab
    }()
    
    
    deinit
    {
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
    
    
}








